package com.RMP
{
    import flash.display.*;
    import flash.events.*;
    import com.greensock.*;

    public class ProjPaginatedMenu extends Sprite
    {
        var rArrowOn:Boolean;
        var btnsArray:Array;
        var ellipseArray:Array;
        var fadeSpeed:Number;
        var numBtns:int;
        var currNavId:int;
        var myRoot:MovieClip;
        var rArrow:Sprite;
        var visBtnsArray:Array;
        var lArrowOn:Boolean;
        var navItem:NavItem;
        var ellipse:NavItem;
        var lArrow:Sprite;

        public function ProjPaginatedMenu(param1:MovieClip, param2:Number, param3:int) : void
        {
            myRoot = param1;
            fadeSpeed = param2;
            numBtns = param3;
            currNavId = 0;
            btnsArray = new Array();
            buildCol();
            buildArrows();
            currNavId = 0;
            switchCols(currNavId);
            myRoot.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            doResize();
            return;
        }

        function arrowRollOver(event:MouseEvent) : void
        {
            if (event.currentTarget.useHandCursor != false)
            {
                TweenMax.to(event.currentTarget, fadeSpeed, {tint:myRoot.hotColor, overwrite:true});
            }
            return;
        }

        function updateNums() : void
        {
            var _loc_1:* = undefined;
            var _loc_2:int = 0;
            _loc_1 = btnsArray.length - 1;
            _loc_2 = 0;
            while (_loc_2 < btnsArray.length)
            {
                
                if (_loc_2 == 0)
                {
                    btnsArray[_loc_2].visible = true;
                }
                else if (_loc_2 == (btnsArray.length - 1))
                {
                    btnsArray[_loc_2].visible = true;
                }
                else if (Math.abs(currNavId - _loc_2) == 1 || currNavId - _loc_2 == 0)
                {
                    btnsArray[_loc_2].visible = true;
                }
                else
                {
                    btnsArray[_loc_2].visible = false;
                }
                _loc_2++;
            }
            if (currNavId == 0 && btnsArray.length >= 3)
            {
                btnsArray[2].visible = true;
            }
            if (currNavId == (btnsArray.length - 1) && btnsArray.length >= 3)
            {
                btnsArray[btnsArray.length - 3].visible = true;
            }
            if (currNavId >= 3 && currNavId <= btnsArray.length - 4)
            {
                ellipseArray[0].visible = true;
                ellipseArray[1].visible = true;
            }
            else if (btnsArray.length == 4)
            {
                ellipseArray[0].visible = false;
                ellipseArray[1].visible = false;
            }
            else if (currNavId >= 4)
            {
                ellipseArray[0].visible = true;
                ellipseArray[1].visible = false;
            }
            else if (currNavId <= btnsArray.length - 4)
            {
                ellipseArray[0].visible = true;
                ellipseArray[1].visible = false;
            }
            else
            {
                ellipseArray[0].visible = false;
                ellipseArray[1].visible = false;
            }
            if (currNavId == (btnsArray.length - 1))
            {
                rArrowOn = false;
                rArrow.useHandCursor = false;
                TweenMax.to(rArrow, fadeSpeed, {tint:12500670, overwrite:true});
            }
            else
            {
                if (rArrowOn == false)
                {
                    TweenMax.to(rArrow, fadeSpeed, {tint:myRoot.coldColor, overwrite:true});
                }
                rArrowOn = true;
                rArrow.useHandCursor = true;
            }
            if (currNavId == 0)
            {
                lArrowOn = false;
                lArrow.useHandCursor = false;
                TweenMax.to(lArrow, fadeSpeed, {tint:12500670, overwrite:true});
            }
            else
            {
                if (lArrowOn == false)
                {
                    TweenMax.to(lArrow, fadeSpeed, {tint:myRoot.coldColor, overwrite:true});
                }
                lArrow.useHandCursor = true;
                lArrowOn = true;
            }
            doResize();
            return;
        }

        function doResize() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            visBtnsArray = new Array();
            _loc_1 = 0;
            if (btnsArray != null && btnsArray.length > 0)
            {
                lArrow.x = Math.round(3);
                rArrow.x = Math.round(lArrow.x + 11);
                var _loc_4:* = Math.round(7 * myRoot.unitW / 75);
                rArrow.y = Math.round(7 * myRoot.unitW / 75);
                lArrow.y = _loc_4;
                _loc_2 = 0;
                while (_loc_2 < btnsArray.length)
                {
                    
                    if (btnsArray[_loc_2].visible == true)
                    {
                        visBtnsArray.push(btnsArray[_loc_2]);
                        if (btnsArray[(_loc_2 + 1)] != undefined)
                        {
                            if (btnsArray[(_loc_2 + 1)].visible == false)
                            {
                                visBtnsArray.push(ellipseArray[_loc_1]);
                                ellipseArray[_loc_1].bg.visible = false;
                                ellipseArray[_loc_1].bg.useHandCursor = false;
                                ellipseArray[_loc_1].bg.mouseEnabled = false;
                                ellipseArray[_loc_1].bg.buttonMode = false;
                                _loc_1++;
                            }
                        }
                    }
                    _loc_2++;
                }
                _loc_3 = 0;
                while (_loc_3 < visBtnsArray.length)
                {
                    
                    var _loc_4:* = myRoot.unitW / 75;
                    visBtnsArray[_loc_3].field.scaleY = myRoot.unitW / 75;
                    visBtnsArray[_loc_3].field.scaleX = _loc_4;
                    if (_loc_3 == 0)
                    {
                        visBtnsArray[_loc_3].x = Math.round(rArrow.x + 11);
                    }
                    else
                    {
                        visBtnsArray[_loc_3].x = visBtnsArray[(_loc_3 - 1)].x + visBtnsArray[(_loc_3 - 1)].field.width;
                    }
                    visBtnsArray[_loc_3].bg.width = visBtnsArray[_loc_3].field.width;
                    visBtnsArray[_loc_3].bg.height = visBtnsArray[_loc_3].field.height + 3;
                    _loc_3++;
                }
            }
            return;
        }

        function buildCol() : void
        {
            var _loc_1:int = 0;
            buildEllipse();
            _loc_1 = 0;
            while (_loc_1 < numBtns)
            {
                
                btnsArray[_loc_1] = new NavItem(myRoot, String((_loc_1 + 1)), String((_loc_1 + 1)), _loc_1);
                addChild(btnsArray[_loc_1]);
                btnsArray[_loc_1].addEventListener(MouseEvent.MOUSE_OVER, btnRollOver, false, 0, true);
                btnsArray[_loc_1].addEventListener(MouseEvent.MOUSE_OUT, btnRollOut, false, 0, true);
                btnsArray[_loc_1].addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
                _loc_1++;
            }
            if (btnsArray.length == 1)
            {
                btnsArray[0].visible = false;
            }
            return;
        }

        function arrowRollOut(event:MouseEvent) : void
        {
            if (event.currentTarget.useHandCursor != false)
            {
                TweenMax.to(event.currentTarget, fadeSpeed, {tint:myRoot.coldColor, overwrite:true});
            }
            return;
        }

        function switchCols(param1:Number) : void
        {
            updateNums();
            fadeBtns();
            dispatchEvent(new PaginatedMenuClick(currNavId));
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        function btnRollOver(event:MouseEvent) : void
        {
            if (event.currentTarget.idNum != currNavId)
            {
                TweenMax.to(event.currentTarget.field, fadeSpeed, {tint:myRoot.overColor, overwrite:true});
            }
            else
            {
                TweenMax.to(event.currentTarget.field, fadeSpeed, {tint:myRoot.hotColor, overwrite:true});
            }
            return;
        }

        function fadeBtns() : void
        {
            var _loc_1:int = 0;
            if (btnsArray.length > 1)
            {
                _loc_1 = 0;
                while (_loc_1 < btnsArray.length)
                {
                    
                    if (_loc_1 != currNavId)
                    {
                        TweenMax.to(btnsArray[_loc_1].field, fadeSpeed, {tint:myRoot.coldColor, overwrite:true});
                    }
                    else
                    {
                        TweenMax.to(btnsArray[_loc_1].field, fadeSpeed, {tint:myRoot.hotColor, overwrite:true});
                    }
                    _loc_1++;
                }
            }
            else
            {
                btnsArray[0].visible = false;
            }
            return;
        }

        function buildEllipse() : void
        {
            var _loc_1:int = 0;
            ellipseArray = new Array();
            _loc_1 = 0;
            while (_loc_1 < 2)
            {
                
                ellipse = new NavItem(myRoot, "...", "...", _loc_1);
                ellipse.name = "ellipse" + _loc_1;
                addChild(ellipse);
                ellipseArray.push(ellipse);
                _loc_1++;
            }
            return;
        }

        function btnRollOut(event:MouseEvent) : void
        {
            if (event.currentTarget.idNum != currNavId)
            {
                TweenMax.to(event.currentTarget.field, fadeSpeed, {tint:myRoot.coldColor, overwrite:true});
            }
            return;
        }

        function buildArrows() : void
        {
            lArrow = new Sprite();
            lArrow.graphics.beginFill(myRoot.hotColor, 0);
            lArrow.graphics.drawRect(-3, -7, 11, 25);
            lArrow.graphics.endFill();
            lArrow.graphics.beginFill(myRoot.coldColor, 100);
            lArrow.graphics.drawRect(0, 4, 1, 1);
            lArrow.graphics.drawRect(1, 3, 1, 3);
            lArrow.graphics.drawRect(2, 2, 1, 5);
            lArrow.graphics.drawRect(3, 1, 1, 7);
            lArrow.graphics.drawRect(4, 0, 1, 9);
            lArrow.graphics.endFill();
            lArrow.buttonMode = true;
            lArrow.name = "lArrow";
            lArrow.addEventListener(MouseEvent.MOUSE_OVER, arrowRollOver);
            lArrow.addEventListener(MouseEvent.MOUSE_OUT, arrowRollOut);
            lArrow.addEventListener(MouseEvent.CLICK, arrowClick);
            rArrow = new Sprite();
            rArrow.graphics.beginFill(myRoot.hotColor, 0);
            rArrow.graphics.drawRect(-3, -7, 11, 25);
            rArrow.graphics.endFill();
            rArrow.graphics.beginFill(myRoot.coldColor, 100);
            rArrow.graphics.drawRect(0, 0, 1, 9);
            rArrow.graphics.drawRect(1, 1, 1, 7);
            rArrow.graphics.drawRect(2, 2, 1, 5);
            rArrow.graphics.drawRect(3, 3, 1, 3);
            rArrow.graphics.drawRect(4, 4, 1, 1);
            rArrow.graphics.endFill();
            rArrow.buttonMode = true;
            rArrow.name = "rArrow";
            rArrow.addEventListener(MouseEvent.MOUSE_OVER, arrowRollOver);
            rArrow.addEventListener(MouseEvent.MOUSE_OUT, arrowRollOut);
            rArrow.addEventListener(MouseEvent.CLICK, arrowClick);
            if (btnsArray.length > 1)
            {
                addChild(lArrow);
                addChild(rArrow);
            }
            return;
        }

        function btnClick(event:MouseEvent) : void
        {
            currNavId = event.currentTarget.idNum;
            switchCols(currNavId);
            return;
        }

        public function fadeInShell() : void
        {
            currNavId = 0;
            switchCols(currNavId);
            visible = true;
            TweenMax.to(this, fadeSpeed, {alpha:1, overwrite:true});
            return;
        }

        function arrowClick(event:MouseEvent) : void
        {
            if (event.currentTarget.useHandCursor != false)
            {
                if (event.currentTarget.name == "rArrow")
                {
                    var _loc_3:* = currNavId + 1;
                    currNavId = _loc_3;
                    if (currNavId == btnsArray.length)
                    {
                        currNavId = btnsArray.length - 1;
                    }
                }
                else
                {
                    var _loc_3:* = currNavId - 1;
                    currNavId = _loc_3;
                    if (currNavId < 0)
                    {
                        currNavId = 0;
                    }
                }
                if (currNavId == (btnsArray.length - 1))
                {
                    rArrow.useHandCursor = false;
                }
                else
                {
                    rArrow.useHandCursor = true;
                }
                if (currNavId == 0)
                {
                    lArrow.useHandCursor = false;
                }
                else
                {
                    lArrow.useHandCursor = true;
                }
                switchCols(currNavId);
            }
            return;
        }

    }
}
